var a = document.querySelectorAll('.layui-side-scroll a')
var b = document.querySelectorAll('.layui-body')
var tbody = document.querySelector('tbody')
a.forEach((item, index) => {
  item.addEventListener('click', () => {
    b.forEach(item1 => {
      console.log(item1);
      item1.classList.remove('sh')
    })
    b[index].classList.add('sh')
  })
})
//渲染
function apply() {
  $.ajax({
    type: "get",
    url: "http://47.94.4.201/index.php/index/teacher/teacherlist",
    dataType: 'json',
    success: (a) => {
      tbody.innerHTML = ''
      var b = a.data.data
      b.forEach(item => {
        if (item.sex == 1) {
          item.sex = '女'
        } else if (item.sex == 0) {
          item.sex = '男'
        } else {
          item.sex = '其他'
        }
        $('tbody').append(`
           <tr>
              <td>${item.id}</td>
              <td>${item.name}</td>
              <td>${item.nick}</td>
              <td>${item.birthdata}</td>
              <td>${item.sex}</td>
              <td>${item.iphone}</td>
              <td>
              <button class='but' onclick='cha(${item.id})'>查看</button>
              <button class='but' onclick='xiu(${item.id})'>编辑</button>
              <button class='but1' onclick='del(${item.id})'>注销</button>
              </td>
          </tr>
        `)
      })
    },
    error: (e) => {
      console.log(e);
    }
  })
}
apply()
//删除
function del(id) {
  $.ajax({
    type: "get",
    url: "http://47.94.4.201/index.php/index/teacher/delteacher",
    data: { id: id },
    dataType: 'json',
    success: (a) => {
      apply()
    },
    error: (e) => {
      console.log(e);
    }
  })
}
//添加
function add() {
  // location.assign('./添加.html')
  $('.w').css('display', 'block')
}
//修改
var xiu_id
function xiu(id) {
  $('.ww').css('display', 'block')
  xiu_id = id
}

function xiu1(id) {
  $.ajax({
    type: "post",
    url: 'http://47.94.4.201/index.php/index/index/upteacher',
    data: {
      id: xiu_id,
      name: $('.boxs-input').eq(0).val(),
      birthdata: $('.boxs-input').eq(1).val(),
      sex: $('.boxs-input').eq(2).val()
    },
    dataType: 'json',
    success: (a) => {
      console.log(a);
      if (a.msg == '参数不能为空') {
        alert(`${a.msg}`)
      } else if (a.msg == '修改成功') {
        alert(`${a.msg}`)
        $('.ww').css('display', 'none')
      }
      apply()
    },
    error: (e) => {
      console.log(e);
    }
  })
}
//添加
function add1() {
  $.ajax({
    type: "post",
    url: "http://47.94.4.201/index.php/index/index/addteacher",
    data: {
      name: $('.boxs input').eq(0).val(),
      nick: $('.boxs input').eq(1).val(),
      birthdata: $('.boxs input').eq(2).val(),
      iphone: $('.boxs input').eq(3).val(),
      teachertype: $('.boxs input').eq(4).val(),
      sex: $('.boxs input').eq(5).val(),
    },
    dataType: 'json',
    success: (a) => {
      if (a.msg == '参数不能为空') {
        alert(`${a.msg}`)
      } else if (a.msg == '添加成功') {
        alert(`${a.msg}`)
        $('.w').css('display', 'none')
      }
      apply()
    },
    error: (e) => {
      console.log(e);
    }
  })
}
$('.w').css('display', 'none')
//搜索
function sou() {
  var inp = document.querySelector('.inp')
  $.ajax({
    type: "get",
    url: "http://47.94.4.201/index.php/index/teacher/getsearch",
    data: {
      name: inp.value,
    },
    dataType: 'json',
    success: (a) => {
      tbody.innerHTML = ''
      a.data.data.forEach(item => {
        tbody.innerHTML += `
       <tr>
              <td>${item.id}</td>
              <td>${item.name}</td>
              <td>${item.nick}</td>
              <td>${item.birthdata}</td>
              <td>${item.sex}</td>
              <td>${item.iphone}</td>
              <td>
              <button class='but' onclick='cha(${item.id})'>查看</button>
              <button class='but' onclick='xiu(${item.id})'>编辑</button>
              <button class='but1' onclick='del(${item.id})'>注销</button>
              </td>
          </tr>
      `
      })
    },
    error: (e) => {
      console.log(e);
    }
  })
}
//个人
$('#ks').css('display', 'none')
function del1() {
  localStorage.setItem('name', JSON.stringify($('#box input').eq(0).val()))
  localStorage.setItem('img', JSON.stringify($('#box input').eq(1).val()))
  localStorage.setItem('nick', JSON.stringify($('#box input').eq(2).val()))
  localStorage.setItem('sex', JSON.stringify($('#box input').eq(3).val()))
  localStorage.setItem('birthdata', JSON.stringify($('#box input').eq(4).val()))
  localStorage.setItem('iphone', JSON.stringify($('#box input').eq(5).val()))
  localStorage.setItem('emails', JSON.stringify($('#box input').eq(6).val()))
  localStorage.setItem('indate', JSON.stringify($('#box input').eq(7).val()))
  localStorage.setItem('mydes', JSON.stringify($('#box input').eq(8).val()))
  $.ajax({
    type: "post",
    url: "http://47.94.4.201/index.php/index/index/mycenter",
    data: {
      name: $('#box input').eq(0).val(),
      userid: 1,
      img: $('#box input').eq(1).val(),
      nick: $('#box input').eq(2).val(),
      sex: $('#box input').eq(3).val(),
      birthdata: $('#box input').eq(4).val(),
      iphone: $('#box input').eq(5).val(),
      emails: $('#box input').eq(6).val(),
      indate: $('#box input').eq(7).val(),
      mydes: $('#box input').eq(8).val(),
    },
    success: (a) => {
      console.log(a);
      if (a.msg == '添加成功') {
        alert(`${a.msg}`)
        $('.w2').css('display', 'none')
        $('#ks').css('display', 'block')
      } else {
        alert(`${a.msg}`)
      }
    },
    error: (e) => {
      console.log(e);
    }
  })
}
function fna() {
  $('#ks').css('display', 'block')
  var name = JSON.parse(localStorage.getItem('name'))
  var img = JSON.parse(localStorage.getItem('img'))
  var nick = JSON.parse(localStorage.getItem('nick'))
  var sex = JSON.parse(localStorage.getItem('sex'))
  var birthdata = JSON.parse(localStorage.getItem('birthdata'))
  var iphone = JSON.parse(localStorage.getItem('iphone'))
  var emails = JSON.parse(localStorage.getItem('emails'))
  var indate = JSON.parse(localStorage.getItem('indate'))
  var mydes = JSON.parse(localStorage.getItem('mydes'))

  $.ajax({
    type: "get",
    url: 'http://47.94.4.201/index.php/index/index/getmycenter',
    data: {
      userid: 1,
    },
    dataType: "json",
    success: (a) => {
      if (a.msg) {
        $('#name').html(name)
        $('#img').html(`<img src="${img}">`)
        $('#nick').html(nick)
        if (sex == '1') {
          sex = '女'
          $('#sex').html(sex)
        } if (sex == '0') {
          sex = '男'
          $('#sex').html(sex)
        }
        $('#birthdata').html(birthdata)
        $('#iphone').html(iphone)
        $('#emails').html(emails)
        $('#indate').html(indate)
        $('#mydes').html(mydes)
      }
    },
    error: (e) => {
      console.log(e);
    }
  })
}
//退出
var abc = document.querySelector('.bac')
var abc1 = document.querySelector('.bac1')

abc.addEventListener('click', () => {
  location.assign('./首页.html')
})
abc1.addEventListener('click', () => {
  location.assign('./首页.html')
})
function bac() {
  location.assign('./首页.html')
}
function bac1() {
  location.assign('./首页.html')
}
function bc() {
  location.assign('./首页.html')
}
function bc1() {
  location.assign('./首页.html')
}

var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
var option;

option = {
  tooltip: {
    trigger: 'item'
  },
  legend: {
    top: '5%',
    left: 'center'
  },
  series: [
    {
      name: 'Access From',
      type: 'pie',
      radius: ['40%', '70%'],
      avoidLabelOverlap: false,
      padAngle: 5,
      itemStyle: {
        borderRadius: 10
      },
      label: {
        show: false,
        position: 'center'
      },
      emphasis: {
        label: {
          show: true,
          fontSize: 40,
          fontWeight: 'bold'
        }
      },
      labelLine: {
        show: false
      },
      data: [
        { value: 1048, name: 'Search Engine' },
        { value: 735, name: 'Direct' },
        { value: 580, name: 'Email' },
        { value: 484, name: 'Union Ads' },
        { value: 300, name: 'Video Ads' }
      ]
    }
  ]
};

option && myChart.setOption(option);

///


var xhr = new XMLHttpRequest()
xhr.open('get', 'http://47.94.4.201/index.php/index/teacher/teacherlist')
xhr.send()
xhr.onload = () => {
  var a = JSON.parse(xhr.response).data.data[0]
  var b = JSON.parse(xhr.response).data.data[1]
  var c = JSON.parse(xhr.response).data.data[2]
  var d = JSON.parse(xhr.response).data.data[3]
  var a1 = JSON.parse(xhr.response).data.data[4]
  var a2 = JSON.parse(xhr.response).data.data[5]
  var w = JSON.parse(xhr.response).data.data

  var chartDom = document.getElementById('mai');
  var myChart = echarts.init(chartDom);
  var option;

  option = {
    title: {

    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#6a7985'
        }
      }
    },
    legend: {
      data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
    },
    toolbox: {
      feature: {
        saveAsImage: {}
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        boundaryGap: false,
        data: [a.name, b.name, c.name, d.name, a1.name, a2.name,]
      }
    ],
    yAxis: [
      {
        type: 'value'
      }
    ],
    series: [
      {
        name: a.name,
        type: 'line',
        stack: 'Total',
        areaStyle: {},
        emphasis: {
          focus: 'series'
        },
        data: [a.birthdata, b.birthdata, c.birthdata, d.birthdata, a1.birthdata, a2.birthdata]
      },
      {
        name: b.name,
        type: 'line',
        stack: 'Total',
        areaStyle: {},
        emphasis: {
          focus: 'series'
        },
        data: [220, 182, 191, 234, 290, 330, 310]
      },
      {
        name: c.name,
        type: 'line',
        stack: 'Total',
        areaStyle: {},
        emphasis: {
          focus: 'series'
        },
        data: [150, 232, 201, 154, 190, 330, 410]
      },
      {
        name: d.name,
        type: 'line',
        stack: 'Total',
        areaStyle: {},
        emphasis: {
          focus: 'series'
        },
        data: [320, 332, 301, 334, 390, 330, 320]
      },
    ]
  };

  option && myChart.setOption(option);
}
xhr.onerror = () => {

}
//个人
function ger() {
  $('.w2').css('display', 'block')
}
//------------------------------------------------------------------
const apiUrl = "http://47.94.4.201/index.php/index/teacher/teacherlist";
let currentPage = 1;
const pageLimit = 10;

// 请求数据
async function fetchData(page) {
  if (page < 1) return;

  const response = await fetch(`${apiUrl}?page=${page}&pagelimit=${pageLimit}`);
  const result = await response.json();

  if (result.code === 0) {
    const dataList = result.data.data;
    const pageCount = result.data.pagecount;

    if (page > pageCount) return;

    currentPage = page;
    renderList(dataList);
    document.getElementById("current-page").innerText = currentPage;
    document.getElementById("total-pages").innerText = Math.ceil(pageCount / 8);
  }
}
// ---------------------讲师管理列表-----------------------
function renderList(data) {
  const dataListDiv = document.getElementById("data-list");
  data.forEach(item => {
    if (item.sex == 1) {
      item.sex = '女'
    } else if (item.sex == 0) {
      item.sex = '男'
    } else {
      item.sex = '其他'
    }
  })
  dataListDiv.innerHTML = data
    .map(item =>
      `<tr>
          <td>${item.id}</td>
          <td>${item.name}</td>
          <td>${item.nick}</td>
          <td>${item.birthdata}</td>
          <td>${item.sex}</td>
          <td>${item.iphone}</td>
          <td>
          <button class='but' onclick='cha(${item.id})'>查看</button>
          <button class='but' onclick='xiu(${item.id})'>编辑</button>
          <button class='but1' onclick='del(${item.id})'>注销</button>
          </td>
       </tr>`
    )
    .join("");
}
fetchData(1); // 初始化加载第一页

